package com.shinbada.common.tree;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.shinbada.common.dict.Dict;
import com.shinbada.common.dict.EnumDict;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * @author initcap
 * @date Created in 2020/9/23 10:27.
 */
@AllArgsConstructor
@Getter
@Dict("tree-modle")
public enum TreeModle implements EnumDict<String> {
    @JsonProperty("line")
    line("列优先"),
    @JsonProperty("level")
    level("层优先");

    private final String text;

    /**
     * 用来解决当使用@RequestBody进行接收参数时无法序列化问题
     *
     * @param text 前端传过来的值
     * @return 找到的枚举
     */
    @JsonCreator
    public static TreeModle fromText(String text) {
        for (TreeModle state : TreeModle.values()) {
            if (state.getValue().equals(text)) {
                return state;
            }
        }
        throw new IllegalArgumentException("unknown code");
    }

    @Override
    public String getValue() {
        return name();
    }

}
